package com.aaa.springboot_rbac.service.impl;

import com.aaa.springboot_rbac.dao.StudentDao;
import com.aaa.springboot_rbac.entity.Student;
import com.aaa.springboot_rbac.service.StudentService;
import com.aaa.springboot_rbac.util.CustomException;
import com.aaa.springboot_rbac.util.Page;
import com.aaa.springboot_rbac.util.ResultStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @FileName: StudentServiceImpl
 * @Description:
 * @Author: 张钰哲
 * @CreateTime: 2025/8/22 16:00
 * @Version: 1.0.0
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;

    @Override
    public int add(Student student) {
        return studentDao.add(student);
    }

    @Override
    public int update(Student student) {
        return studentDao.update(student);
    }

    @Override
    public int deleteById(Integer id) {
        return studentDao.deleteById(id);
    }

    @Override
    public Student queryById(Integer id) {
        return studentDao.queryById(id);
    }

    @Override
    public PageInfo queryByPage(Page<Student> page) {
        Integer pageNo = page.getPageNo();
        Integer pageSize = page.getPageSize();
        if (pageNo == null || pageSize == null||pageNo == 0||pageSize == 0) {
            // 说明传入的 参数 的 数值 不符合规范。 直接抛出异常,程序直接停止运行
            //throw new IllegalArgumentException("参数异常！！！！");

            throw new CustomException(ResultStatus.PARAM_ISNULL.getStatusCode(), ResultStatus.PARAM_ISNULL.getStatusMsg());
        }
        // 调用PageHelper设置参数方法，开始分页
        PageHelper.startPage(pageNo,pageSize);
        // 获取page类的 data 属性，该属性 是Dept类型
        Student data = page.getData();
        // 调用dao层查询
        List<Student> deptList = studentDao.queryByParam(data);
        // 使用查询结果封装成返回对象
        return new PageInfo(deptList);
    }
}
